<template>
  <el-dialog
    :title="!dataForm.id ? $t('Common.Add') : $t('Common.Edit')"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="名称"></el-input>
    </el-form-item>
    <el-form-item label="上级" prop="parentId">
      <el-select v-model="dataForm.parentId" placeholder="请选择">
          <el-option
            v-for="item in dataList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
    </el-form-item>
    <el-form-item label="快捷" prop="isQuick">
      <el-switch v-model="dataForm.isQuick" :active-value="1" :inactive-value="0"></el-switch>
    </el-form-item>
    <el-form-item label="状态" prop="state">
      <el-switch v-model="dataForm.state" :active-value="1" :inactive-value="0" active-text="启用" class="switch" inactive-text="禁用"></el-switch>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{ $t('Common.Cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmit()">{{ $t('Common.Confirm') }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataList: [],
        dataForm: {
          id: 0,
          name: '',
          parentId: 0,
          sellerId: '',
          isQuick: 0,
          isShop: '',
          state: 1,
          creatTime: ''
        },
        dataRule: {
          name: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ],
          parentId: [
            { required: true, message: '上级(0默认顶级)不能为空', trigger: 'blur' }
          ],
          sellerId: [
            { required: true, message: '商家不能为空', trigger: 'blur' }
          ],
          isQuick: [
            { required: true, message: '快捷(0禁用1启用)不能为空', trigger: 'blur' }
          ],
          isShop: [
            { required: true, message: '商品（0没有1有）不能为空', trigger: 'blur' }
          ],
          state: [
            { required: true, message: '状态（0禁用1启用）不能为空', trigger: 'blur' }
          ],
          creatTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataList = [{
          name:"顶级目录",
          id:0
        }];
        this.$http({
          url: this.$http.adornUrl('/hotel/thotelfloorroomconsume/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': 1,
            'limit': 999,
            'parentId': 0
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList.push(
              ...data.page.list
            )
          } else {
            this.dataList = []
          }
        })
      },
      init (id,type) {

        this.visible = true
        this.getDataList();
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if(type == '1'){
            this.dataForm.id = id || 0
            if (this.dataForm.id) {
              this.$http({
                url: this.$http.adornUrl(`/hotel/thotelfloorroomconsume/info/${this.dataForm.id}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm.name = data.tHotelFloorRoomConsume.name
                  this.dataForm.parentId = data.tHotelFloorRoomConsume.parentId
                  this.dataForm.sellerId = data.tHotelFloorRoomConsume.sellerId
                  this.dataForm.isQuick = data.tHotelFloorRoomConsume.isQuick
                  this.dataForm.isShop = data.tHotelFloorRoomConsume.isShop
                  this.dataForm.state = data.tHotelFloorRoomConsume.state
                  this.dataForm.creatTime = data.tHotelFloorRoomConsume.creatTime
                }
              })
            }
          }else{
            this.dataForm.parentId = id
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/thotelfloorroomconsume/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'parentId': this.dataForm.parentId,
                'sellerId': this.dataForm.sellerId,
                'isQuick': this.dataForm.isQuick,
                'isShop': this.dataForm.isShop,
                'state': this.dataForm.state,
                'creatTime': this.dataForm.creatTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: this.$t('Common.OperationSuccessful'),
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
